package com.yisoo.ubag.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yisoo.ubag.app.feign.IotFeignService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yisoo.ubag.app.entity.OrderEntity;
import com.yisoo.ubag.app.service.OrderService;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.R;



/**
 * 
 *
 * @author mmciel
 * @email 761998179@qq.com
 * @date 2021-04-16 23:19:13
 */
@RestController
@RequestMapping("app/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private IotFeignService iotFeignService;



    //营收管理的表格



//"/order/mapping/control/"
    @RequestMapping("/mapping/control/{boxid}/{bagid}/{control}")
    public R refreshBagData(@PathVariable("boxid") Long boxid,@PathVariable("bagid") Long bagId,@PathVariable("control") Integer control){
        return orderService.runOrderMapping(boxid,bagId,control);
    }

    @RequestMapping("/get/order/data/{uid}/{bagid}")
    public R refreshBagData(@PathVariable("uid") String uid,@PathVariable("bagid") Long bagId){
        return orderService.getOrderStatus(uid,bagId);
    }

    @RequestMapping("/ok/{uid}/{bagid}")
    public R ok(@PathVariable("uid") String uid,@PathVariable("bagid") Long bagId){
        return orderService.ok(uid,bagId);
    }

    @RequestMapping("/lock/{devcode}/{bagid}")
    public R lock(@PathVariable("devcode") String devcode,@PathVariable("bagid") Long bagId){
        return iotFeignService.lock(devcode,bagId);
    }

//    let RefreshBagDataUrl = "/order/refresh/bag/data/"
    @RequestMapping("/refresh/bag/data/{bagid}")
    public R refreshBagData(@PathVariable("bagid") Long bagId){
        return orderService.refreshBagData(bagId);
    }


    //NFC、扫码 快速打开背包
    /**
     *
     * @param uid 用户id
     * @param devSecret 设备id序列
     * @return
     */
    @RequestMapping("/auto/{uid}/{devid}")
    public R auto(@PathVariable("uid") String uid,@PathVariable("devid") String devSecret){
        return orderService.autoOrder(uid,devSecret);
    }

    //获得背包状态
    @RequestMapping("/get/bag/using/{bagid}")
    public R getBagUsing(@PathVariable("bagid") String bagId){
        return iotFeignService.getBagStatus(bagId);
    }
    //更新背包状态
    @RequestMapping("/update/bag/status/{bagid}/{data}")
    public R updateBagStatus(@PathVariable("bagid") Long bagId, @PathVariable("data")Integer data){
        return iotFeignService.updateBagStatus(bagId,data);
    }

    //获得未完成的一个订单
    @RequestMapping("/get/unfinish/{uid}")
    public R getUnfinish(@PathVariable("uid") String uid){
        List<OrderEntity> orderUnfinished = orderService.getOrderUnfinished(uid);
        if (orderUnfinished == null || orderUnfinished.size()<1){
            return R.error("没有订单");
        }else{
            return R.ok().put("data",orderUnfinished.get(0));
        }

    }
//    //废弃的接口
//    //创建订单+打开背包
//    @RequestMapping("/create/{uid}/{devid}")
//    public R createOrder(@PathVariable("uid") String uid,@PathVariable("devid") String devSecret){
//        return orderService.createOrder(uid,devSecret);
//    }

    //打开背包


    /**
     * 列表
     */
    @RequestMapping("/no/list/{uid}")
    //@RequiresPermissions("app:order:list")
    public R list(@PathVariable("uid") Long uid,@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPageByUid(uid,params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/no/list/nofinish/{uid}")
    //@RequiresPermissions("app:order:list")
    public R nofinish(@PathVariable("uid") Long uid,@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPageByUidNofinish(uid,params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/no/get/money/{uid}")
    //@RequiresPermissions("app:order:list")
    public R money(@PathVariable("uid") Long uid){
        return orderService.getMoney(uid);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
    //@RequiresPermissions("app:order:info")
    public R info(@PathVariable("orderId") Integer orderId){
		OrderEntity order = orderService.getById(orderId);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("app:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("app:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("app:order:delete")
    public R delete(@RequestBody Integer[] orderIds){
		orderService.removeByIds(Arrays.asList(orderIds));

        return R.ok();
    }

}
